package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chixing.entity.Customer;
import com.chixing.entity.LoginCustomer;
import com.chixing.mapper.CustomerMapper;
import com.chixing.service.ICustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chixing.util.Result;
import com.chixing.util.TokenUtil;
import org.apache.lucene.search.suggest.analyzing.FSTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shy
 * @since 2022-10-20
 */
@Service
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private CustomerMapper customerMapper;
    @Override
    public Result login(Customer customer) {
        QueryWrapper<Customer> wrapper=new QueryWrapper<>();
        System.out.println(customer.getCustTelno());
        System.out.println(customer.getCustPassword());
        wrapper.eq("cust_telno",customer.getCustTelno());
        wrapper.eq("cust_password",customer.getCustPassword());

        Customer loginUser=customerMapper.selectOne(wrapper);
        System.out.println("查询到此时登录的用户:"+loginUser);

        if (loginUser!=null){
            //JWT创建token
            LoginCustomer loginCustomer =  new LoginCustomer(loginUser.getCustId(),loginUser.getCustName());

            String token = TokenUtil.getToken(loginCustomer);
            System.out.println("service token:"+token);

            return Result.success("登录成功",token);
        }
        else
            return Result.fail("登陆失败",null);

    }

    @Override
    public Result regist(Long custTelno ,Long custPassword) {
        //1. 查看手机号是否存在

        QueryWrapper<Customer> wrapper=new QueryWrapper<>();
        wrapper.eq("cust_telno",custTelno);
        Customer registUser=customerMapper.selectOne(wrapper);
        if(registUser ==null){
            Customer customer = new Customer();
            customer.setCustTelno(custTelno);
            customer.setCustPassword(custPassword);
            int rows = customerMapper.insert(customer);
            if(rows >0)
                return  Result.success("注册成功",registUser);
            else
                return  Result.fail("注册失败",null);
        }else{
            return Result.fail("该手机号已经注册过",registUser);
        }




    }

    @Override
    public Long getTelById(Integer custId) {

        return customerMapper.selectById(custId).getCustTelno();
    }

    @Override
    public Long getCustCerNumById(Integer custId) {
        return customerMapper.selectById(custId).getCustCertificatesNumber();
    }

    @Override
    public String getCustDriverLicense(Integer custId) {
        return customerMapper.selectById(custId).getCustDriverLicense();
    }

//    @Override
//     public Result regist1(Long custTelno ,Long custPassword) {
//        Customer customer = new Customer();
//        customer.setCustTelno(custTelno);
//        customer.setCustPassword(custPassword);
//        int insert = customerMapper.insert(customer);
//        System.out.println(customer);
//
//
//if (insert>0){
//    HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
//    objectObjectHashMap.put("customer",customer);
//    return  Result.success("添加成功",objectObjectHashMap);
//}
//        return  Result.fail("添加失败",null);
//    }

    @Override
    public Customer getall(Integer custId) {

        Customer customer = customerMapper.selectById(custId);

        return customer;

    }
}
